<?php
/**
 * Created by PhpStorm.
 * 店铺模型
 * User: caicm
 * Date: 2017/6/15
 * Time: 19:51
 */

namespace WMApi\Model;


use Think\Model;

class WmStoreModel extends Model {

    /**
     * 获取商家店铺数量
     * Created By caicm
     * @param int $seller_id
     * @param int $store_id
     * @return mixed
     */
    public function find_store_num($seller_id=0, $store_id=0){

        $where = array(
            'seller_id' => $seller_id,
            'store_id' => $store_id
        );
        if($seller_id==0) unset($where['seller_id']);
        if($store_id==0) unset($where['store_id']);

        return $this->where($where)->count();

    }

    /**
     * 设置店铺为当前店铺
     * Created By caicm
     * @param int $seller_id
     * @param int $store_id
     * @param int $is_default
     * @return bool
     */
    public function edit_store_is_default($seller_id=0, $store_id=0, $is_default=0){

        $where = array(
            'seller_id' => $seller_id,
            'store_id' => $store_id
        );
        if($seller_id==0) unset($where['seller_id']);
        if($store_id==0) unset($where['store_id']);

        $data = array(
            'is_default' => $is_default
        );

        return $this->where($where)->data($data)->save();
    }

    /**
     * 获取商家店铺列表
     * Created By caicm
     * @param int $seller_id
     * @return mixed
     */
    public function find_store_list($seller_id=0){

        $where = array(
            's.seller_id' => $seller_id
        );

        $field = '
                    s.add_time,
                    s.store_id,
                    s.store_name,
                    s.store_img,
                    s.province_name,
                    s.city_name,
                    s.district_name,
                    s.address,
                    s.address_name,
                    s.store_sw,
                    s.store_audit,
                    s.is_default,
                    s.store_introduce,
                    s.start_time,
                    s.end_time,
                    s.shipping_way,
                    s.shipping_radius,
                    s.contacts,
                    s.contacts_phone,
                    s.shipping_fee,
                    s.starting_money,
                    s.is_on_line,
                    s.sign_building,
                    c1.classify_name as classify_name1,
                    c2.classify_name as classify_name2';

        $join  = ' left join sdx_wm_store_classify as c2 on s.classify_id=c2.classify_id';
        $join .= ' left join sdx_wm_store_classify as c1 on s.parent_classify_id=c1.classify_id';

        return $this->alias('s')->field($field)->join($join)->where($where)->order('s.store_id desc')->select();
    }

    /**
     * 获取商家店铺列表
     * Created By caicm
     * @param int $seller_id
     * @return mixed
     */
    public function find_default_store($seller_id=0){

        $where = array(
            's.seller_id' => $seller_id,
            's.is_default' => 1
        );

        $field = '
                    s.add_time,
                    s.store_id,
                    s.store_name,
                    s.store_img,
                    s.province_name,
                    s.city_name,
                    s.district_name,
                    s.address,
                    s.address_name,
                    s.store_sw,
                    s.store_audit,
                    s.is_default,
                    s.store_introduce,
                    s.start_time,
                    s.end_time,
                    s.shipping_way,
                    s.shipping_radius,
                    s.contacts,
                    s.contacts_phone,
                    s.shipping_fee,
                    s.starting_money,
                    c1.classify_name as classify_name1,
                    c2.classify_name as classify_name2';

        $join  = ' left join sdx_wm_store_classify as c2 on s.classify_id=c2.classify_id';
        $join .= ' left join sdx_wm_store_classify as c1 on s.parent_classify_id=c1.classify_id';

        return $this->alias('s')->field($field)->join($join)->where($where)->find();
    }


    /**
     * 店铺添加
     * Created By caicm
     * @param int $seller_id 商家id
     * @param string $store_name 店铺名称
     * @param string $store_introduce 店铺描述
     * @param int $classify_id 店铺运营分类id（二级）
     * @param int $parent_classify_id 店铺运营分类id（一级）
     * @param array $address_data 地址信息
     * @param string $sign_building 地标
     * @param string $address_name  门牌号
     * @param string $contacts 联系人
     * @param int $contacts_phone 联系人电话
     * @param string $start_time 店铺运营开始时间
     * @param string $end_time 店铺运营结束时间
     * @param int $starting_money 起送金额
     * @param int $store_sw 店铺开关（1：营业，2：休息）
     * @param int $is_default 是否默认店铺
     * @param string $store_img 店铺图片
     * @param int $shipping_fee 配送费用
     * @param int $shipping_way 配送方式 1-周边配 2-干线全城配
     * @param int $shipping_radius 配送半径（公里）
     * @param int $scc_id 物流取货点id
     * @return mixed
     */
    public function add_store($seller_id=0, $store_name='', $store_introduce='', $classify_id=0, $parent_classify_id=0, $address_data=array(), $sign_building='', $address_name='', $contacts='', $contacts_phone=0, $start_time='', $end_time='', $starting_money=0, $store_sw=0, $is_default=0, $store_img='', $shipping_fee=0, $shipping_way=0, $shipping_radius=0, $scc_id=0){

        $data = array(
            'seller_id' => $seller_id,
            'store_name' => $store_name,
            'store_introduce' => $store_introduce,
            'classify_id' => $classify_id,
            'parent_classify_id' => $parent_classify_id,
            'sign_building' => $sign_building,
            'address_name' => $address_name,
            'contacts' => $contacts,
            'contacts_phone' => $contacts_phone,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'starting_money' => $starting_money,
            'store_sw' => $store_sw,
            'is_default' => $is_default,
            'store_img' => $store_img,
            'shipping_fee' => $shipping_fee,
            'shipping_way' => $shipping_way,
            'shipping_radius' => $shipping_radius,
            'scc_id' => $scc_id,

            'store_audit' => 2,
            'is_on_line' => 2,
            'add_time' => gmtime(),

            'province' => $address_data['province'],
            'city' => $address_data['city'],
            'district' => $address_data['district'],
            'province_name' => $address_data['province_name'],
            'city_name' => $address_data['city_name'],
            'district_name' => $address_data['district_name'],
            'address' => $address_data['address'],
            'longitude' => $address_data['longitude'],
            'latitude' => $address_data['latitude'],
            'source' => get_source(),
            'version' => get_version()
        );

        return $this->data($data)->add();
    }

    /**
     * 编辑店铺
     * Created By caicm
     * @param int $seller_id 商家id
     * @param int $store_id 店铺id
     * @param string $store_name 店铺名称
     * @param string $store_introduce 店铺描述
     * @param int $classify_id 店铺运营分类id（二级）
     * @param int $parent_classify_id 店铺运营分类id（一级）
     * @param array $address_data 地址信息
     * @param string $sign_building 地标
     * @param string $address_name  门牌号
     * @param string $contacts 联系人
     * @param int $contacts_phone 联系人电话
     * @param string $start_time 店铺运营开始时间
     * @param string $end_time 店铺运营结束时间
     * @param int $starting_money 起送金额
     * @param int $store_sw 店铺开关（1：营业，2：休息）
     * @param string $store_img 店铺图片
     * @param int $shipping_fee 配送费用
     * @param int $shipping_way 配送方式 1-周边配 2-干线全城配
     * @param int $shipping_radius 配送半径（公里）
     * @param int $is_default  是否默认店铺 0-否 1-是
     * @param int $store_audit 店铺审核状态（1：通过，2：未审核，3：不通过）
     * @return bool
     */
    public function edit_store($seller_id=0, $store_id=0, $store_name='', $store_introduce='', $classify_id=0, $parent_classify_id=0, $address_data=array(), $sign_building='', $address_name='', $contacts='', $contacts_phone=0, $start_time='', $end_time='', $starting_money=0, $store_sw=0, $store_img='', $shipping_fee=0, $shipping_way=0, $shipping_radius=0, $is_default=0, $store_audit=1){

        $data = array(
            'store_name' => $store_name,
            'store_introduce' => $store_introduce,
            'contacts' => $contacts,
            'contacts_phone' => $contacts_phone,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'starting_money' => $starting_money,
            'store_sw' => $store_sw,
            'is_default' => $is_default,
            'store_img' => $store_img,
            'shipping_fee' => $shipping_fee,
            'update_time' => gmtime(),

        );

        if($store_img == '') unset($data['store_img']);

        //店铺审核不同过时，可修改下面内容
        if($store_audit == 3){
            $data_base = array(
                'classify_id' => $classify_id,
                'parent_classify_id' => $parent_classify_id,
                'sign_building' => $sign_building,
                'address_name' => $address_name,
                'shipping_way' => $shipping_way,
                'shipping_radius' => $shipping_radius,
                'province' => $address_data['province'],
                'city' => $address_data['city'],
                'district' => $address_data['district'],
                'province_name' => $address_data['province_name'],
                'city_name' => $address_data['city_name'],
                'district_name' => $address_data['district_name'],
                'address' => $address_data['address'],
                'longitude' => $address_data['longitude'],
                'latitude' => $address_data['latitude'],
                'store_audit' => 2
            );

            $data = array_merge($data, $data_base);
        }


        $where = array(
            'store_id' => $store_id,
            'seller_id' => $seller_id
        );

        return $this->where($where)->data($data)->save();

    }

    /**
     * 店铺经营分类id（顶级分类）
     * Created By caicm
     * @param int $seller_id
     * @param int $store_id
     * @return mixed
     */
    public function get_parent_classify_id($seller_id=0, $store_id=0){

        $where = array(
            'store_id' => $store_id,
            'seller_id' => $seller_id
        );

        return $this->where($where)->getField('parent_classify_id');

    }

    /**
     * 获取商家默认店铺id
     * Created By caicm
     * @param int $seller_id
     * @return mixed
     */
    public function get_default_store_id($seller_id=0){

        $where = array(
            'is_default' => 1,
            'seller_id' => $seller_id
        );

        return $this->where($where)->getField('store_id');

    }

    /**
     * 获取店铺详情
     * Created By caicm
     * @param int $seller_id
     * @param int $store_id
     * @return mixed
     */
    public function find_store_info($seller_id=0, $store_id=0){

        $where = array(
            'store_id' => $store_id,
            'seller_id' => $seller_id
        );

        return $this->where($where)->find();

    }

    /**
     * 获取物流取货点id
     * Created By caicm
     * @param int $store_id
     * @return mixed
     */
    public function find_scc_id($store_id=0){
        $where = array(
            'store_id' => $store_id,
        );

        return $this->where($where)->getField('scc_id');
    }

}